
#include "spl.h"

#include <QPainter>
#include <QPainterPath>

spl::spl(QPixmap backg, QWidget *parent)
    : QWidget(parent)
    , dwAngle(0)
{
    background = backg;
     brushColor = Qt::green;
}

spl::~spl()
{

}

void spl::reDraw()
{
    QPainter painter(this);
    QPainterPath path;
    background = background.scaled(this->size(), Qt::IgnoreAspectRatio);

    switch(dwAngle)
    {
    case 90:
        painter.translate(background.width(), 0);
        painter.rotate(dwAngle); //˳ʱ����ת90��
        painter.drawPixmap(0,0,background.width(),background.height(),background);
        painter.resetTransform();
        painter.setPen(QPen(Qt::white, Qt::SolidLine));
        painter.setBrush(brushColor);
        path.addText((this->width() - strText.size()*20*this->width()/60) / 2, this->height()-6*this->width()/60, QFont("Arial", 24*this->width()/60, QFont::Bold), strText);
        painter.drawPath(path);
        break;
    case 180:
        painter.translate(background.width(), background.height());
        painter.rotate(dwAngle);
        painter.drawPixmap(0,0,background.width(),background.height(),background);
        painter.resetTransform();
        painter.setPen(QPen(Qt::white, Qt::SolidLine));
        painter.setBrush(brushColor);
        path.addText((0), this->height()-22*this->width()/60, QFont("Arial", 24*this->width()/60, QFont::Bold), strText);
        painter.drawPath(path);
        break;
    case 270:
        painter.translate(0, background.height());
        painter.rotate(dwAngle);
        painter.drawPixmap(0,0,background.width(),background.height(),background);
        painter.resetTransform();
        painter.setPen(QPen(Qt::white, Qt::SolidLine));
        painter.setBrush(brushColor);
        path.addText((this->width() - strText.size()*18*this->width()/60) / 2, 0+24*this->width()/60, QFont("Arial", 24*this->width()/60, QFont::Bold), strText);
        painter.drawPath(path);
        break;
    default:
        painter.drawPixmap(0,0,background.width(),background.height(),background);
        painter.resetTransform();
        painter.setPen(QPen(Qt::white, Qt::SolidLine));
        painter.setBrush(brushColor);
        path.addText((this->width() - strText.size()*20*this->width()/60) + 2, (this->height()-18*this->width()/60), QFont("Arial", 24*this->width()/60, QFont::Bold), strText);
        painter.drawPath(path);
        break;
    }
}

void spl::paintEvent(QPaintEvent *)
{
    reDraw();
}

void spl::setAngle(int angle)
{
    dwAngle = angle;
}

void spl::setText(QString txt, const QColor & color)
{
    strText = txt;
    brushColor = color;
}
